﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using Autorent.Desktop.Main;
using Autorent.Desktop.Model.AutorentMngrServiceReference;
using Autorent.Desktop.Modules.Autoes.Services;
using Autorent.Desktop.Modules.Events;
using Autorent.Desktop.Modules.Clients.ViewModels;
using Autorent.Desktop.Modules.Clients.Views;
using Autorent.Desktop.Modules.Offices.Services;
using Autorent.Desktop.Modules.Orders.Services;
using Autorent.Desktop.Modules.Orders.Views;

namespace Autorent.Desktop.Modules.Orders.ViewModels
{
    public class UpdateAutoOrderViewModel
    {
        #region Public Properies

        public UpdateAutoOrderView View { get; set; }

        public AutoOrder AutoOrder { get; set; }

        public ObservableCollection<Office> Offices
        {
            get
            {
                return _officesRepository.GetOffices();
            }
        }

        public ObservableCollection<Auto> Autoes
        {
            get
            {
                return _autoRepository.GetAutoes();
            }
        }

        public ObservableCollection<AutoClass> AutoClasses
        {
            get
            {
                return _autoClassesRepository.GetAutoClasses();
            }
        }

        #endregion

        #region Constructors

        public UpdateAutoOrderViewModel(UpdateAutoOrderView view, AutoOrder autoOrder,
            IAutoOrdersRepository autoOrderRepository, IOfficesRepository officesRepository, 
            IAutoesRepository autoRepository, IAutoClassesRepository autoClassesRepository,
            IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            View = view;
            view.Model = this;

            _container = container;
            _regionManager = regionManager;
            _autoOrderRepository = autoOrderRepository;
            _officesRepository = officesRepository;
            _autoRepository = autoRepository;
            _autoClassesRepository = autoClassesRepository;
            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<ClientCreatedOrUpdatedEvent>().Subscribe(new System.Action<Client>(ClientCreatedEventHandler));
            _eventAggregator.GetEvent<ViewClosedEvent>().Subscribe(new System.Action<System.Type>(ViewClosedEventHandler));

            this.AutoOrder = autoOrder;
        }

        #endregion

        #region Commands

        public ICommand SaveAutoOrder
        {
            get
            {
                return new RelayCommand(param => saveAutoOrder());
            }
        }

        public ICommand UpdateClient
        {
            get
            {
                return new RelayCommand(param => updateClient());
            }
        }

        #endregion

        #region Private variables

        private readonly IAutoOrdersRepository _autoOrderRepository;
        private readonly IOfficesRepository _officesRepository;
        private readonly IAutoesRepository _autoRepository;
        private readonly IAutoClassesRepository _autoClassesRepository;
        private readonly IRegionManager _regionManager;
        private readonly IUnityContainer _container;
        private readonly IEventAggregator _eventAggregator;

        #endregion

        #region Private Methods

        private void saveAutoOrder()
        {
            _autoOrderRepository.UpdateAutoOrder(AutoOrder);

            _regionManager.Regions["DetailsRegion"].Remove(View);

            _eventAggregator.GetEvent<ClientCreatedOrUpdatedEvent>().Unsubscribe(ClientCreatedEventHandler);
            _eventAggregator.GetEvent<ViewClosedEvent>().Unsubscribe(ViewClosedEventHandler);
        }

        private void updateClient()
        {
            UpdateAutoOrderView updateAutoOrderView = (UpdateAutoOrderView)_regionManager.Regions["DetailsRegion"].GetView("UpdateAutoOrderView");

            if (updateAutoOrderView != null)
            {
                updateAutoOrderView.Visibility = System.Windows.Visibility.Collapsed;

                UpdateClientView view = _container.Resolve<UpdateClientViewModel>(new ParameterOverrides() { {"client", this.AutoOrder.Client}}).View;

                _regionManager.Regions["DetailsRegion"].Add(view, "UpdateClientView");
                _regionManager.Regions["DetailsRegion"].Activate(view);
            }
        }

        private void ClientCreatedEventHandler(Client client)
        {
            this.AutoOrder.Client = client;

            UpdateAutoOrderView updateAutoOrderView = (UpdateAutoOrderView)_regionManager.Regions["DetailsRegion"].GetView("UpdateAutoOrderView");
            if (updateAutoOrderView != null)
            {
                updateAutoOrderView.Visibility = Visibility.Visible;
                _regionManager.Regions["DetailsRegion"].Activate(updateAutoOrderView);
            }
        }

        private void ViewClosedEventHandler(Type viewType)
        {
            if (viewType == typeof(CreateClientView))
            {
                UpdateAutoOrderView updateAutoOrderView = (UpdateAutoOrderView)_regionManager.Regions["DetailsRegion"].GetView("UpdateAutoOrderView");
                if (updateAutoOrderView != null)
                {
                    updateAutoOrderView.Visibility = Visibility.Visible;
                    _regionManager.Regions["DetailsRegion"].Activate(updateAutoOrderView);
                }
            }
        }

        #endregion
    }
}
